package com.onec.service.trade.controller;

import com.onec.service.api.dto.request.gateway.BizContent;
import com.onec.service.api.dto.response.BaseResponse;
import com.onec.service.api.enums.ServiceApiEnum;
import com.onec.service.api.enums.error.TradeErrorEnum;
import com.onec.service.api.exception.TradeException;
import com.onec.service.trade.service.online.base.OnlineBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 交易数据统一出入口
 *
 * @author ONEC
 */
@Api(value = "agent", tags = "公共入口")
@RestController
@RequestMapping(value = "/agent")
public class AgentController extends BaseTradeController {

    private static final Logger log = LoggerFactory.getLogger(AgentController.class);

    /**
     * 创建交易订单.
     *
     * @param bizContent
     * @return
     */
    @ApiOperation(value = "/api/online", tags = "订单信息公共入口")
    @PostMapping(value = "/api/online")
    public BaseResponse onlineApi(@RequestBody BizContent bizContent) {

        if (!ServiceApiEnum.isInclude(bizContent.getService())) {
            throw new TradeException(TradeErrorEnum.NO_SERVER_ERROR);
        }
        ServiceApiEnum requestMethod = ServiceApiEnum.valueOf(bizContent.getService());
        OnlineBaseService onlineBaseService = applicationContext
                .getBean(requestMethod.name(), OnlineBaseService.class);
        BaseResponse baseResponse = onlineBaseService.execute(bizContent);
        return baseResponse;
    }
}
